<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.css">
    <script src="/socket.io/socket.io.js"></script>
    <script src="/jquery/dist/jquery.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        body {
            background-image: url("/images/meinv01.jpg");
            background-size: 1920px;
        }

        .box {
            opacity: 0.7;
            width: 1200px;
            height: 600px;
            margin: 100px auto;
            /* border: 2px solid #ccc; */
        }

        .box1 {
            width: 100px;
            height: 100%;
            background-color: #333;
            float: left;
        }

        .box1 img {
            margin-top: 20px;
            margin-left: 20px;
        }

        /* 按钮 */
        .box1 button {
            margin-left: 20px;
            margin-top: 10px;
            background-color: pink;
        }

        .box1 a {
            margin-left: 20px;
            margin-top: 10px;
            background-color: pink;
        }

        /* 中间部分大盒子 */
        .box2 {
            width: 296px;
            height: 100%;
            background-color: #EAE8E7;
            float: left;
        }

        /* 中间顶部搜索栏 */
        .box2 .search {
            width: 100%;
            height: 80px;
            background-color: #F5F5F5;
        }

        .box .inp {
            width: 80%;
            height: 30px;
            border-radius: 5px;
            margin-top: 25px;
            margin-left: 20px;
            text-align: center;
        }

        /* 中间部分联系人 */
        .box2 .person {
            width: 100%;
            height: 70px;
            border-bottom: 1px solid #ccc;
        }

        .box2 .person img {
            margin-top: 10px;
            margin-left: 10px;
            vertical-align: center;
        }

        .box2 .person span {
            font-size: 16px;
            font-weight: 400;

        }

        /* 右侧聊天室 */
        .box3 {
            width: 600px;
            height: 100%;
            background-color: #F5F5F5;
            float: left;

        }

        /* 右侧顶部 */
        .box3-top {
            width: 100%;
            height: 80px;
            border-left: 1px solid #ccc;
        }

        .box3-top span {
            line-height: 80px;
            margin-left: 20px;
            font-size: 20px;
        }

        /* 右侧中间 */
        .box3-middle {
            width: 100%;
            height: 400px;
            border-top: 1px solid #ccc;
            overflow: auto;
        }

        #kuang {
            display: none;
        }

        /* 对话 */

        .right {
            width: 100%;
            height: 50px;
        }

        .right-user-speak {
            height: 50px;
            padding: 6px 8px;
            background-color: pink;
            border-radius: 5px;
        }

        .left {
            width: 100%;
            height: 50px;
        }

        .left-user {
            float: right;
        }

        .left-user-speak {
            /* height: 50px; */
            padding: 6px 8px;
            background-color: #9EEA6A;
            border-radius: 5px;
            float: right;
        }


        /* 右侧底部 */

        .box3-bottom {
            width: 100%;
            height: 120px;
            background-color: #FFFFFF;
            border: 1px solid #ccc;
            position: relative;
        }

        textarea {
            width: 100%;
            height: 70px;
            border: 1px solid #e1e1e1;
        }

        /* 发送按钮 */
        .box3-bottom button {
            background-color: pink;
        }

        .his {
            width: 200px;
            height: 100%;
            background-color: #F5F5F5;
            border-left: 1px solid #ccc;
            float: left;
            display: none;
        }

        .his-top {
            width: 100%;
            height: 80px;
            border-bottom: 1px solid #ccc;
        }

        .up{
            text-align: center;
        }
        .outt{
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="" alt="">

        <!-- 左侧登录人员 -->
        <div class="box1">
            <h1><img src="/images/1.jpg" alt="" width="60px"></h1>

            <!-- <a class="btn btn-danger" href="/login">登录</a><br> -->
            <a href="/out" class="btn btn-info out">退出</a>
            <!-- 左侧登陆人员结束 -->
        </div>


        <!-- 中间在线人员 -->
        <div class="box2">
            <!-- 中间搜索栏 -->
            <div class="search">
                <input class="inp" type="text" placeholder="搜索">
            </div>
            <div class="person">
                <img src="/images/2.webp" alt="" width="50px">
                <span>小丽子</span>
            </div>

            <div class="person">
                <img src="/images/3.webp" alt="" width="50px">
                <span>小弘</span>
            </div>

            <div class="person">
                <img src="/images/4.webp" alt="" width="50px">
                <span>组长</span>
            </div>

            <div class="person">
                <img src="/images/5.jfif" alt="" width="50px">
                <span>小玉</span>
            </div>

            <div class="person">
                <img src="/images/6.jfif" alt="" width="50px">
                <span>小淼子</span>
            </div>

            <div class="person">
                <img src="/images/bird.jpg" alt="" width="50px">
                <span>轩子</span>
            </div>
            <!-- 中间在线人员结束 -->
        </div>


        <!-- 右侧聊天窗口 -->
        <div class="box3">
            <!-- 顶部 名字+设置窗口 -->
            <div class="box3-top">
                <span>雷神的马大锤</span>
            </div>

            <div class="box3-middle">


            </div>

            <div id="kuang">
                <% for(let i=0;i<data.length;i++){ %>
                <div class="left">
                    <span class="left-user"><b><%=data[i].name%></b>:</span>
                    <span class="left-user-speak">周末给你做油焖大虾~</span>
                </div>
                <% } %>

                <div class="right">
                    <span class="right-user"><b>小马</b>:</span>
                    <span class="right-user-speak">再加一碗臭粉！！</span>
                </div>

                <div class="up"><b>小丽</b>上线啦~</div>
                <div class="outt"><b>小丽</b>离线了</div>
                
            </div>

            <div class="box3-bottom">
                <textarea id="text" name="text" style="resize:none"></textarea>
                <button class="btn btn-info send">发送</button>
                <button id="history" class="btn btn-info">历史</button>
            </div>

            <!-- 右侧聊天窗口结束 -->
        </div>
        <div class="his">
            <div class="his-top">
                <p>历史记录</p>
            </div>
            <div class="his-bottom">
                <ul>

                </ul>
            </div>
        </div>
        <!-- 总盒子结束 -->
    </div>
    <script>
        let socket = io.connect("/");
        //   let name = prompt("请输入你的名字");
        let name = '<%=data[0].username%>'
        $('.send').click(function () {
            //获取表单中的信息
            let v = $('textarea').val();
            //console.log(v);
            //把自己的对话框放到 class="top"的元素中
            //1.我们要找到我们要克隆的节点
            //要复制(克隆)的节点.clone()
            let me = $('#kuang .left').clone();
            //修改 节点中的名字和发送的内容
            me.find('.left-user b').html(name);
            me.find('.left-user-speak').html(v);
            $('.box3-middle').append(me);
            setTimeout(function () {
                ($('.box3-middle').children("div:last-child")[0]).scrollIntoView();
            })
            socket.emit('msx020703', { 'name': name, 'msg': v })
            $('textarea').val("");
        })

        //点击回车发送信息
        $(window).keydown(e => {
            if (e.keyCode == 13) {
                //让前边的元素执行一次参数中的事件
                $('.send').trigger('click');
                return false;
            }
        })
        // 接收服务端广播出来的信息
        socket.on("msx020703", res => {
            //克隆right 节点
            let o = $('#kuang .right').clone();
            o.find('.right-user-speak').html(res.msg);
            o.find('.right-user b').html(res.name);
            $('.box3-middle').append(o);
            console.log(res);
        })


        // 进入聊天室
        socket.emit('up', { name: name })
        socket.on('up', res => {
            let up = $('#kuang .up').clone();
            up.html(res.name + "上线了");
            $('.box3-middle').append(up);
        })

        // 退出聊天室
        $('.out').on('click', function () {
            socket.emit('outt', { name: name })
        })
        socket.on('outt', res => {
            let out = $('#kuang .outt').clone();
            out.html(res.name + "下线了");
            $('.box3-middle').append(out);
        })

        $('#history').on('click', function () {
            $('.his').toggle();
        })

        let a = true;
        $("#history").click(function () {
            if (a == true) {
                $.get("/his-bottom", {}, function (res) {
                    for (let i = 0; i < res.ls.length; i++) {
                        $(".his-bottom ul").append(`<li><span>${res.ls[i].name}:</span>${res.ls[i].msg}</li>`)
                    }
                })
                a = false
            } else {
                $(".his-bottom ul").html("");
                a = true;
            }
        })
    </script>
</body>

</html>